﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_subsystem;
using web_search;

namespace BookManageSystem
{
    public partial class FormReturnBook : Form
    {
        public FormReturnBook()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        //归还按钮
        private void button2_Click(object sender, EventArgs e)
        {
            //检查选中的图书是否可归还
            if (LabelStatus.Text == "已归还")
            {
                MessageBox.Show("选定的该书在相应时间段内已归还！请检查后归还。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //确认窗口
            DialogResult result = MessageBox.Show("确认归还？", "确认归还", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)//确认
            {
                DaoAdmin dao = new DaoAdmin();
                dao.connect();
                string sql = $"update BorrowRec set ActualReturnTime='{DateTime.Now}' where BookID='{LabelBookID.Text}' and ActualReturnTime IS NULL";
                if (dao.Execute(sql) > 0)
                {
                    MessageBox.Show("归还成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("归还出错！请重试", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                return;
            }
        }
        //加载借阅记录
        void LoadBorrowRec()
        {
            //获取登录人
            string userid = FormLogin.id;
            Daouser dao = new Daouser();
            dao.connect();

            string sql = $"select BorrowID,BookID,BookName,AuthorName,BorrowTime,EstiReturnTime,BookStatus from V_UserBorrowList where UserID='{userid}'";

            SqlDataReader reader = dao.read(sql);
            //加入到表格中,先清空再加载
            dgv_return.Rows.Clear();
            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[6].ToString() == "0")
                {
                    bookstatus = "已归还";
                }
                else if (reader[6].ToString() == "1")
                {
                    bookstatus = "未归还";
                }
                else if (reader[6].ToString() == "2")
                {
                    bookstatus = "已逾期";
                }
                dgv_return.Rows.Add(
                    reader[0].ToString(),
                    reader[1].ToString(),
                    reader[2].ToString(),
                    reader[3].ToString(),
                    reader[4].ToString(), 
                    reader[5].ToString(),
                    bookstatus
                    );
            }
        }

        private void FormReturnBook_Load(object sender, EventArgs e)
        {
            LoadBorrowRec();
        }
        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //获取当前选中的行
            if (dgv_return.CurrentRow == null || dgv_return.CurrentRow.Cells[0].Value == null)
            { 
                LabelBookID.Text = "NULL";
                LabelBorrowDate.Text = "NULL";
                LabelTitle.Text = "NULL";
                LabelStatus.Text = "NULL";
                return;
            }

            string ISBN_tmp = dgv_return.CurrentRow.Cells[0].Value.ToString();//当前选中的图书编号
            string title = dgv_return.CurrentRow.Cells[1].Value.ToString();//当前选中的图书书名
            //显示选中的图书信息
            LabelBookID.Text = dgv_return.CurrentRow.Cells[1].Value.ToString();
            LabelTitle.Text = dgv_return.CurrentRow.Cells[2].Value.ToString();
            LabelBorrowDate.Text = dgv_return.CurrentRow.Cells[4].Value.ToString();
            LabelStatus.Text= dgv_return.CurrentRow.Cells[6].Value.ToString();

        }

        private void dgv_return_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            //重新加载状态
            LoadBorrowRec();
            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
